import { createStore } from 'vuex';

// 初始状态
const state = {
  cities: [
    {
      id: 1,
      name: '巴黎',
      country: '法国',
      description: '浪漫之都，艺术与时尚的殿堂，拥有埃菲尔铁塔、卢浮宫等世界著名景点。',
      image: 'https://picsum.photos/id/1031/800/500',
      rating: 4.8,
      population: '2140000',
      language: '法语',
      currency: '欧元',
      attractions: [
        { id: 101, name: '埃菲尔铁塔', image: 'https://picsum.photos/id/1031/400/300', rating: 4.9 },
        { id: 102, name: '卢浮宫', image: 'https://picsum.photos/id/1033/400/300', rating: 4.8 },
        { id: 103, name: '凯旋门', image: 'https://picsum.photos/id/1035/400/300', rating: 4.7 }
      ],
      testimonials: [
        { id: 201, name: '李明', avatar: 'https://picsum.photos/id/1005/100/100', text: '巴黎真的太美了，浪漫的氛围让人陶醉，埃菲尔铁塔夜景尤其震撼！', rating: 5 },
        { id: 202, name: '王芳', avatar: 'https://picsum.photos/id/1006/100/100', text: '卢浮宫的艺术品让人叹为观止，建议预留充足时间参观。', rating: 4 }
      ]
    },
    {
      id: 2,
      name: '东京',
      country: '日本',
      description: '现代与传统完美融合的大都市，拥有丰富的美食文化和独特的购物体验。',
      image: 'https://picsum.photos/id/1036/800/500',
      rating: 4.7,
      population: '13960000',
      language: '日语',
      currency: '日元',
      attractions: [
        { id: 201, name: '东京塔', image: 'https://picsum.photos/id/1037/400/300', rating: 4.6 },
        { id: 202, name: '浅草寺', image: 'https://picsum.photos/id/1038/400/300', rating: 4.8 },
        { id: 203, name: '新宿御苑', image: 'https://picsum.photos/id/1039/400/300', rating: 4.7 }
      ],
      testimonials: [
        { id: 203, name: '张伟', avatar: 'https://picsum.photos/id/1012/100/100', text: '东京的现代建筑和传统文化形成鲜明对比，非常值得一游！', rating: 5 },
        { id: 204, name: '刘静', avatar: 'https://picsum.photos/id/1014/100/100', text: '美食种类繁多，尤其是寿司和拉面，让人回味无穷。', rating: 4 }
      ]
    },
    {
      id: 3,
      name: '纽约',
      country: '美国',
      description: '世界金融中心，拥有自由女神像、时代广场等标志性景点，充满活力的不夜城。',
      image: 'https://picsum.photos/id/1040/800/500',
      rating: 4.6,
      population: '8804000',
      language: '英语',
      currency: '美元',
      attractions: [
        { id: 301, name: '自由女神像', image: 'https://picsum.photos/id/1041/400/300', rating: 4.9 },
        { id: 302, name: '时代广场', image: 'https://picsum.photos/id/1042/400/300', rating: 4.7 },
        { id: 303, name: '中央公园', image: 'https://picsum.photos/id/1043/400/300', rating: 4.8 }
      ],
      testimonials: [
        { id: 205, name: '陈浩', avatar: 'https://picsum.photos/id/1015/100/100', text: '纽约的繁华让人印象深刻，尤其是时代广场的夜景，非常震撼！', rating: 5 },
        { id: 206, name: '周琳', avatar: 'https://picsum.photos/id/1016/100/100', text: '中央公园是城市中的一片绿洲，非常适合散步放松。', rating: 4 }
      ]
    },
    {
      id: 4,
      name: '罗马',
      country: '意大利',
      description: '永恒之城，拥有古罗马竞技场、万神殿等众多历史遗迹，感受千年文明的魅力。',
      image: 'https://picsum.photos/id/1044/800/500',
      rating: 4.7,
      population: '2873000',
      language: '意大利语',
      currency: '欧元',
      attractions: [
        { id: 401, name: '古罗马竞技场', image: 'https://picsum.photos/id/1045/400/300', rating: 4.9 },
        { id: 402, name: '万神殿', image: 'https://picsum.photos/id/1046/400/300', rating: 4.8 },
        { id: 403, name: '特莱维喷泉', image: 'https://picsum.photos/id/1047/400/300', rating: 4.7 }
      ],
      testimonials: [
        { id: 207, name: '赵伟', avatar: 'https://picsum.photos/id/1017/100/100', text: '罗马的历史遗迹让人惊叹，仿佛穿越回了古罗马时代。', rating: 5 },
        { id: 208, name: '孙倩', avatar: 'https://picsum.photos/id/1018/100/100', text: '意大利美食非常棒，尤其是披萨和意面，一定要尝试！', rating: 4 }
      ]
    },
    {
      id: 5,
      name: '悉尼',
      country: '澳大利亚',
      description: '澳大利亚最大城市，拥有悉尼歌剧院、悉尼港大桥等标志性建筑，海滩风光迷人。',
      image: 'https://picsum.photos/id/1048/800/500',
      rating: 4.8,
      population: '5312000',
      language: '英语',
      currency: '澳元',
      attractions: [
        { id: 501, name: '悉尼歌剧院', image: 'https://picsum.photos/id/1049/400/300', rating: 4.9 },
        { id: 502, name: '悉尼港大桥', image: 'https://picsum.photos/id/1050/400/300', rating: 4.7 },
        { id: 503, name: '邦迪海滩', image: 'https://picsum.photos/id/1051/400/300', rating: 4.8 }
      ],
      testimonials: [
        { id: 209, name: '吴杰', avatar: 'https://picsum.photos/id/1019/100/100', text: '悉尼歌剧院的建筑设计非常独特，是澳大利亚的标志性景点。', rating: 5 },
        { id: 210, name: '郑华', avatar: 'https://picsum.photos/id/1020/100/100', text: '邦迪海滩的风景很美，是放松身心的好地方。', rating: 4 }
      ]
    },
    {
      id: 6,
      name: '北京',
      country: '中国',
      description: '中国的首都，拥有悠久的历史和丰富的文化遗产，如故宫、长城等著名景点。',
      image: 'https://picsum.photos/id/1052/800/500',
      rating: 4.9,
      population: '21540000',
      language: '中文',
      currency: '人民币',
      attractions: [
        { id: 601, name: '故宫博物院', image: 'https://picsum.photos/id/1053/400/300', rating: 4.9 },
        { id: 602, name: '八达岭长城', image: 'https://picsum.photos/id/1054/400/300', rating: 4.9 },
        { id: 603, name: '颐和园', image: 'https://picsum.photos/id/1055/400/300', rating: 4.8 }
      ],
      testimonials: [
        { id: 211, name: '陈晨', avatar: 'https://picsum.photos/id/1021/100/100', text: '故宫的建筑气势恢宏，展示了中国古代皇家的辉煌。', rating: 5 },
        { id: 212, name: '林芳', avatar: 'https://picsum.photos/id/1022/100/100', text: '长城的壮观让人震撼，不愧是世界七大奇迹之一。', rating: 5 }
      ]
    }
  ],
  favorites: JSON.parse(localStorage.getItem('travelFavorites')) || [],
  searchQuery: '',
  isLoading: false
};

// 获取器
const getters = {
  allCities: state => state.cities,
  cityById: state => id => state.cities.find(city => city.id === id),
  favoriteCities: state => state.favorites,
  isCityFavorite: state => id => state.favorites.includes(id),
  searchResults: state => {
    if (!state.searchQuery) return state.cities;
    const query = state.searchQuery.toLowerCase();
    return state.cities.filter(city => 
      city.name.toLowerCase().includes(query) || 
      city.country.toLowerCase().includes(query) ||
      city.description.toLowerCase().includes(query)
    );
  },
  isLoading: state => state.isLoading
};

// 变更（同步操作）
const mutations = {
  ADD_TO_FAVORITES(state, cityId) {
    if (!state.favorites.includes(cityId)) {
      state.favorites.push(cityId);
      localStorage.setItem('travelFavorites', JSON.stringify(state.favorites));
    }
  },
  REMOVE_FROM_FAVORITES(state, cityId) {
    state.favorites = state.favorites.filter(id => id !== cityId);
    localStorage.setItem('travelFavorites', JSON.stringify(state.favorites));
  },
  SET_SEARCH_QUERY(state, query) {
    state.searchQuery = query;
  },
  SET_LOADING(state, value) {
    state.isLoading = value;
  }
};

// 动作（异步操作）
const actions = {
  addToFavorites({ commit }, cityId) {
    commit('ADD_TO_FAVORITES', cityId);
  },
  removeFromFavorites({ commit }, cityId) {
    commit('REMOVE_FROM_FAVORITES', cityId);
  },
  setSearchQuery({ commit }, query) {
    commit('SET_SEARCH_QUERY', query);
  },
  fetchCities({ commit }) {
    // 模拟API调用
    commit('SET_LOADING', true);
    setTimeout(() => {
      commit('SET_LOADING', false);
    }, 1000);
  }
};

export default createStore({
  state,
  getters,
  mutations,
  actions
});
